<!--
 * @Author: wjk
 * @Date: 2020-06-30 14:40:19
 * @LastEditors: wjk
 * @LastEditTime: 2020-08-27 14:30:24
 * @description: file content
-->
<template>
  <base-page-view class-name="home-container" :show-to-top="false">
    <box-content ref="boxContent" :theme="theme" :show-box-icon="showBoxIcon" :box-data="boxData" />
  </base-page-view>
</template>
<script>
import './index.scss'
import boxContent from '../components/boxContent/index'
export default {
  components: {
    boxContent
  },
  data() {
    return {
      showBoxIcon: true,
      boxData: {
        banner_list: [],
        boxPackage: [],
        box_gift: [],
        boxPlan: [],
        boxCloud: [],
        box_module: {},
        boxPartner: [],
        partner_module: {}
      },
      theme: {
        navbar_backgroud: '',
        banner_background: '',
        function_background: ''
      }
    }
  },
  created() {
    this.fetchBoxData()
  },
  onPageScroll(res) {
    this.showBoxIcon = false
    clearInterval(this.shareTimer)
    this.shareTimer = setTimeout(() => {
      this.showBoxIcon = true
    }, 500)
  },
  methods: {
    // 获取箱起详细数据
    async fetchBoxData(flag) {
      this.refreshFlag = !!flag
      const resBanner = await this.$api.earnApi.getBoxBanner()
      if (resBanner.success) {
        this.boxData.banner_list = resBanner.data
      }
      const resEarn = await this.$api.earnApi.getEarning()
      if (resEarn.success && resEarn.data) {
        const { subject_module_list, box_gift, box_module, partner_module, box_stock_details } = resEarn.data
        this.boxData = {
          ...this.boxData,
          boxPackage: subject_module_list ? subject_module_list.filter(i => i.subject_list[0].type === 9) : [], // 箱起礼包
          box_gift,
          boxPlan: subject_module_list ? subject_module_list.filter(i => i.subject_list[0].type === 5) : [], // 箱起计划
          boxCloud: subject_module_list ? subject_module_list.filter(i => i.subject_list[0].type === 13) : [], // 箱起云仓
          box_stock_details,
          box_module,
          boxPartner: subject_module_list ? subject_module_list.filter(i => i.subject_list[0].type === 2) : [], // 箱起城市合伙人
          partner_module
        }
      }
      setTimeout(() => {
        uni.stopPullDownRefresh()
        this.refreshFlag = false
      }, 500)
    }
  },
  onPullDownRefresh() {
    this.fetchBoxData(true)
    if (this.$refs.boxContent) {
      this.$refs.boxContent.getRole()
    }
  }
}
</script>
